Ext.namespace("Site");
Site.List = function() {
		this.site_num = new Ext.form.TextField({
			fieldLabel: '基站编号',
			blankText: '请填写基站编号',
			maxLength:10
		});
		this.site_title = new Ext.form.TextField({
			fieldLabel: '基站名称',
			blankText: '请填写基站名称',
			maxLength:25
		});
		this.site_att = new Ext.form.TextField({
			fieldLabel: '基站属性',
			blankText: '请填写基站属性',
			maxLength:5
		});
		this.site_group=new Ext.form.ComboBox({
			width:100,
			fieldLabel:'基站归属组',
			displayField:'text',
			valueField:'value',
			mode:'local',
			triggerAction: 'all',
			editable: false,
			emptyText:'请选择',						
			store: new Ext.data.SimpleStore({
			fields:['value','text'],
			data:OMS.SITE_GROUP
			})
		});
		this.btnSearch = new Ext.Toolbar.Button({
	        iconCls: 'icon-search',
	        text: '查找',
	        tooltip: {text: '查找'},
	        handler: this.searchSite,
	        scope: this
	    });
	    this.btnReset = new Ext.Toolbar.Button({
	        iconCls: 'icon-modify',
	        text: '重置',
	        tooltip: {text: '清空查询条件'},
	        handler: this.resetSite,
	        scope: this
	    });
	    this.btnAdd = new Ext.Toolbar.Button({
	        iconCls: 'icon-add',
	        text: '新建站点信息',
	        tooltip: {text: '新建站点信息'},
	        handler: this.addSite,
	        scope: this
	    });
	    this.btnSearchOwner = new Ext.Toolbar.Button({
	        iconCls: 'icon-search',
	        text: '查看业主信息',
	        tooltip: {text: '查看业主信息'},
	        handler: this.searchOwner,
	        scope: this
	    });
		function renderFlag(value) {
	        if (value == '0') {
	            return "有";
	        }else if (value == '1') {
	            return "无";
	        }
	    }
		this.cm = new Ext.grid.ColumnModel({
		    defaults: {
		        width: 100,
		        sortable: true,
		        align: 'center'
		    },
		    columns: [
					{id: 'site_id', header: 'ID', width: 50, hidden: true, dataIndex: 'SITE_ID'},
					{header: '站点编号', dataIndex: 'SITE_NUM'},
					{header: '站点名称',dataIndex: 'SITE_TITLE'},
					{header: '地址', dataIndex: 'SITE_POSITION'},
					{header: '站点属性', dataIndex: 'SITE_ATT'},
					{header: '供电单位', dataIndex: 'POWER'},
					{header: '供电是否有保障', dataIndex: 'POWER_ENSURE',renderer : renderFlag},
					{header: '是否有油机转换箱', dataIndex: 'SITE_BOX',renderer :renderFlag},
					{header: '油机转换箱位置', dataIndex: 'SITE_BOX_POSITION'},
					{header: '电力杆路号', dataIndex: 'POWER_POLE_NUM'},
					{header: '经度', dataIndex: 'LONGITUDE'},
			    	{header: '纬度', dataIndex: 'LATITUDE'}
		    ]
		});		

	this.store = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({
			url: '../site/searchSite.form?timesnap=' + Date.parse(new Date()),
			method: 'POST'
		}),
		reader: new Ext.data.JsonReader({
			root: 'result',
			totalProperty: 'total',
			id: 'SITE_ID',
			fields: ['SITE_ID', 'SITE_NUM', 'SITE_TITLE', 'SITE_POSITION', 'SITE_DEP', 'POWER', 'POWER_ENSURE', 'SITE_ATT', 'SITE_BOX', 'SITE_BOX_POSITION', 'LONGITUDE', 'LATITUDE', 'POWER_POLE_NUM', 'NOTE', 'SITE_GROUP']
		}),listeners:{
			 beforeload :{    
	    	  	fn : function(thiz,options){
       	          Ext.apply(thiz.baseParams,
       	          { 
       	          	start: 0,
					limit: 15,
       	          	site_num:this.site_num.getValue(),
					site_title: this.site_title.getValue(),
					site_att: this.site_att.getValue(),
					site_group:this.site_group.getValue()
       	          });    
              },
              scope:this 
	    	}
		}
	});
		this.grid = new Ext.grid.GridPanel({
			title:'站点列表',
			region:'center',
			height:500,
			store:this.store,
			cm:this.cm,
			sm:new Ext.grid.RowSelectionModel({singleSelect:true}),
			stripeRows: true, 
	        loadMask: {msg: '加载中，请稍候...'},
	        tbar:['基站编号',this.site_num,'基站名称',this.site_title,'基站属性',this.site_att,'基站归属组',this.site_group,this.btnSearch,this.btnReset,'->',this.btnAdd,'->',this.btnSearchOwner],
	        bbar:new Ext.PagingToolbar({
				 pageSize: 15,
		         store: this.store,
		         displayInfo: true,
		         displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
		         emptyMsg: "没有记录..."
			}),
	        viewConfig: { forceFit: true },
	        listeners:{
	      	    rowdblclick : function( grid, rowIndex,e){
		      	    Site.Add.instance =new Site.Add();
					Site.Add.instance.initEdit(grid.store.getAt(rowIndex).data);
					Site.Add.instance.show();
				},
				scope:this				
	       }
		});
		Site.List.superclass.constructor.call(this,{			
	        resizable: false,
	        frame:false,
	        items: [this.grid]
		});	
};

Ext.extend(Site.List, Ext.Viewport, {
	resetSite:function(){
		this.site_num.setValue("");
		this.site_title.setValue("");
		this.site_att.setValue("");
		this.site_group.setValue("");
	},
	searchSite:function(){
		this.store.load({params: {
				start: 0,
				limit: 15,
				site_num:this.site_num.getValue(),
				site_title: this.site_title.getValue(),
				site_att: this.site_att.getValue(),
				site_group:this.site_group.getValue()
			}});
	},
	searchOwner : function(){
		var record = this.grid.getSelectionModel().getSelected();
		if(record==null){
			Utils.warn('请选择一条记录！','警告');
			return ;
		}
		Owner.Main.instance = new Owner.Main(record.data.SITE_ID);
		//Owner.Main.instance.searchOwner(record.data.SITE_ID);
		Owner.Main.instance.show();
	},
	addSite:function(){
		Site.Add.instance = new Site.Add();
		Site.Add.instance.show();
	}
});

Ext.onReady(function() {
    Ext.QuickTips.init();
    Ext.BLANK_IMAGE_URL = '../resources/images/default/s.gif';
   	Site.List.instance = new Site.List();   
   	Site.List.instance.searchSite();
   	Site.List.instance.show();
});